//
//  StatusCellRetweetView.swift
//  sinaDemo
//
//  Created by mac on 15/12/19.
//  Copyright © 2015年 zhouxubin. All rights reserved.
//

import UIKit
import SnapKit

class StatusCellRetweetView: UIView {
    
    var bottomConstraint: Constraint?
    
    // 定义外部属性,给属性赋值
    var retweetStatus: Status? {
        didSet {
//            retweetContentLabel.text = "@" + (retweetStatus?.user?.name ?? "") + (retweetStatus?.text ?? "")
            retweetContentLabel.attributedText = retweetStatus?.attributedText
            
            retweetPictureView.imageURLs = retweetStatus?.imageUrls
            // 卸载掉底部的约束
            bottomConstraint?.uninstall()
            // 判断是否有配图
            if let urls = retweetStatus?.imageUrls where urls.count > 0 { // 有配图
                // 更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(retweetPictureView.snp_bottom).constraint
                })
                
            }else { // 没有配图
                // 取消约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(retweetContentLabel.snp_bottom).constraint
                })
            }
            
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(red: 220 / 255.0, green: 220/255.0, blue: 220/255.0, alpha: 1)
        // 初始化子控件
        setUpUI()
    }
    
    // 初始化子控件
    private func setUpUI() {
        addSubview(retweetContentLabel)
        addSubview(retweetPictureView)
        
        // 设置转发微博内容约束
        retweetContentLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(statusCellMargin)
            make.right.equalTo(self.snp_right).offset(statusCellMargin)
            make.top.equalTo(self)
        }
        
        // 设置转发微博配图约束
        retweetPictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(statusCellMargin)
            make.top.equalTo(retweetContentLabel.snp_bottom).offset(statusCellMargin)
        }
        
        // 自己的约束
        self.snp_makeConstraints { (make) -> Void in
            self.bottomConstraint = make.bottom.equalTo(retweetPictureView.snp_bottom).offset(statusCellMargin).constraint
        }
    }
    
    // MARK: 懒加载子视图
    // 转发微博内容
    private lazy var retweetContentLabel: UILabel = UILabel(title: "", color: UIColor.darkGrayColor(), textFont: 14)
    
    // 转发微博配图
    private lazy var retweetPictureView: StatusCellPictureView = StatusCellPictureView()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
